import { defineStore } from 'pinia';
import { Session } from '/@/utils/storage';
import service from '../utils/request';
export const userType = {
	ADMIN: 'SUPER_ADMIN',
	COMPANY: 'COMPANY',
	DEALER: 'DEALER',
	BRANCH: 'BRANCH',
	MAINTAINER: 'MAINTAINER',
};
/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
	state: (): {
		userInfo: UserInfo | null,
		expiresIn: number,
	} => ({
		userInfo: null,
		expiresIn: 0,
	}),
	actions: {
		async parseToken(token: string) {
			if (token.indexOf('.') > 0) {
				const tokenArr = token.split('.');
				const jwt = JSON.parse(decodeURIComponent(escape(atob(tokenArr[1].replace(/-/g, '+').replace(/_/g, '/')))));
				// const userInfo = JSON.parse(Buffer.from(tokenArr[1], 'base64').toString());
				// console.log('userInfo', jwt.userInfo);
				jwt.userInfo.authBtnList = [],
					this.userInfo = jwt.userInfo
				console.log(this.userInfo)
				this.expiresIn = jwt.exp
				return this.userInfo
			} else {
				throw Error('token 格式不正确');
			}

		},
		// 设置用户信息
		async bindPhone(phoneNumber: string) {
			// 设置用户绑定手机号
			console.log("TODO: bindPhone", phoneNumber);
			// service.post('/api/account/bindphone', { phoneNumber }).then((res) => {
			// 	this.userInfo.bindphone = phoneNumber;
			// 	Session.set('userInfo', this.userInfos);
			// });
		},

		async setUserInfos() {
			console.log('TODO: setUserInfos');
			// 存储用户信息到浏览器缓存
			// if (Session.get('userInfo')) {
			// 	this.userInfo = Session.get('userInfo');
			// } else {
			// 	const userInfo: UserInfo = await this.getApiUserInfo();
			// 	this.userInfos = userInfos;
			// }
		},
		// 模拟接口数据
		// https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP
		async getApiUserInfo() {
			return service.get('/api/auth/userInfo').then((res) => {
				const userInfo = {
					username: res.user.account,
					photo:
						res.user.account === 'admin'
							? 'https://img2.baidu.com/it/u=1978192862,2048448374&fm=253&fmt=auto&app=138&f=JPEG?w=504&h=500'
							: 'https://img2.baidu.com/it/u=2370931438,70387529&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
					time: new Date().getTime(),
					roles: res.user.roles || [],
					type: res.user.type || '',
					status: res.user.status,
					org: res.user.org,
					comanyId: res.user.comanyId,
					authBtnList: [],
				};
				console.log('userInfos', res.user, userInfo);
				Session.set('userInfo', userInfo);
				return userInfo;
				// resolve(userInfos);
			});
			// return new Promise((resolve) => {
			// 	setTimeout(() => {
			// 		// 模拟数据，请求接口时，记得删除多余代码及对应依赖的引入
			// 		const username = Cookies.get('username');
			// 		// 模拟数据
			// 		let defaultRoles: Array<string> = [];
			// 		let defaultAuthBtnList: Array<string> = [];
			// 		// admin 页面权限标识，对应路由 meta.roles，用于控制路由的显示/隐藏
			// 		let adminRoles: Array<string> = ['admin'];
			// 		// admin 按钮权限标识
			// 		let adminAuthBtnList: Array<string> = ['btn.add', 'btn.del', 'btn.edit', 'btn.link'];
			// 		// test 页面权限标识，对应路由 meta.roles，用于控制路由的显示/隐藏
			// 		let testRoles: Array<string> = ['common'];
			// 		// test 按钮权限标识
			// 		let testAuthBtnList: Array<string> = ['btn.add', 'btn.link'];
			// 		// 不同用户模拟不同的用户权限
			// 		if (username === 'admin') {
			// 			defaultRoles = adminRoles;
			// 			defaultAuthBtnList = adminAuthBtnList;
			// 		} else {
			// 			defaultRoles = testRoles;
			// 			defaultAuthBtnList = testAuthBtnList;
			// 		}
			// 		// 用户信息模拟数据
			// 		const userInfos = {
			// 			username: username,
			// 			photo:
			// 				username === 'admin'
			// 					? 'https://img2.baidu.com/it/u=1978192862,2048448374&fm=253&fmt=auto&app=138&f=JPEG?w=504&h=500'
			// 					: 'https://img2.baidu.com/it/u=2370931438,70387529&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
			// 			time: new Date().getTime(),
			// 			roles: defaultRoles,
			// 			authBtnList: defaultAuthBtnList,
			// 		};
			// 		Session.set('userInfo', userInfos);
			// 		resolve(userInfos);
			// 	}, 0);
			// });
		},
	},
});
